using UnityEngine;
using System.Collections;

/// \class  gkRigidBodyFragmentExplosion
/// \brief  Explode a game object composed of rigid body at start
[AddComponentMenu("GK/Engine/GameCore/Fx/gkRigidBodyFragmentExplosionAtStart")]
public class gkRigidBodyFragmentExplosionAtStart : MonoBehaviour 
{
	/// Explosion force
	public float ExplosionForce;
	
	/// \brief  Use this for initialization
	void Start() 
	{
		Vector3 f3ExplosionDirection;
		Rigidbody[] rRigidBodyFragments;
		
		// Loop through the rigid bodies of the game object and apply them a radial explosion
		rRigidBodyFragments = GetComponentsInChildren<Rigidbody>();
		foreach(Rigidbody rRigidBodyFragment in rRigidBodyFragments)
		{
			f3ExplosionDirection = rRigidBodyFragment.transform.position - transform.position;
			f3ExplosionDirection.Normalize();
			
			rRigidBodyFragment.transform.parent = null;
			rRigidBodyFragment.AddForceAtPosition(ExplosionForce * f3ExplosionDirection, transform.position);
		}
		
		// Auto destroy
		Destroy(gameObject);
	}
}
